package com.code.config;


import com.code.component.LoginHandlerInterceptor;
import com.code.component.MyLocaleResolver;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.web.servlet.filter.OrderedHiddenHttpMethodFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.*;

/**
 * springMVC配置
 *
 * @author wangjie
 * @version V1.0
 * @date 2020/2/22
 */
//使用WebMvcConfigurerAdapter可以来扩展SpringMVC的功能
//spring boot2.0之后在构造spring配置文件时直接实现WebMvcConfigurer或者直接继承WebMvcConfigurationSupport
// 经测试实现WebMvcConfigurer是没问题，但继承WebMvcConfigurationSupport类是会导致自动配置失效，包括不限于自动配置
// 的静态资源路径（classpath:/META/resources/，classpath:/resources/，classpath:/static/，classpath:/public/）不生效。
@Configuration
public class MyMvcConfig extends WebMvcConfigurationSupport {



    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
       // super.addViewControllers(registry);
        //浏览器发送 /test 请求来到 success
        registry.addViewController("/test").setViewName("success");
        registry.addViewController("/").setViewName("login");
        registry.addViewController("/index.html").setViewName("login");
        registry.addViewController("/main.html").setViewName("dashboard");
        super.addViewControllers(registry);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**").excludePathPatterns("/index.html","/","/user/login","/static/**","/webjars/**");
        super.addInterceptors(registry);
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }

    /**
     * 国际化配置需要
     * @return
     */
    @Bean
    public LocaleResolver localeResolver(){

        return new MyLocaleResolver();
    }

    /**
     * restful风格接口发起put,delete请求需要此过滤器
     * @return
     */
    @Bean
    public OrderedHiddenHttpMethodFilter hiddenHttpMethodFilter() {
        return new OrderedHiddenHttpMethodFilter();
    }


}
